package com.jc.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jc.subject.application.convert.SubjectAnswerDTOConverter;
import com.jc.subject.application.convert.SubjectInfoDTOConverter;
import com.jc.subject.application.convert.SubjectLabelDTOConverter;
import com.jc.subject.application.dto.SubjectInfoDTO;
import com.jc.subject.application.dto.SubjectLabelDTO;
import com.jc.subject.common.entity.PageResult;
import com.jc.subject.common.entity.Result;
import com.jc.subject.domin.entity.SubjectAnswerBO;
import com.jc.subject.domin.entity.SubjectInfoBO;
import com.jc.subject.domin.entity.SubjectLabelBO;
import com.jc.subject.domin.service.SubjectInfoDomainService;
import com.jc.subject.infra.basic.entity.SubjectCategory;
import com.jc.subject.infra.basic.entity.SubjectInfo;
import com.jc.subject.infra.basic.service.SubjectCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * @Description: 刷题controller
 * @Author: ASL_ly
 * @Package: com.jc.subject.application.controller
 * @Project: jc-club
 * @Date: 2024/10/21  12:47
 */
@RestController
@RequestMapping("/subject")
@Slf4j
public class SubjectController {

    @Resource
    private SubjectInfoDomainService subjectInfoDomainService;

    /**
     * 新增题目
     * @param subjectInfoDTO
     * @return
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectInfoDTO subjectInfoDTO){
        try {
            if(log.isInfoEnabled()){
                log.info("SubjectController.add.dto:{}", JSON.toJSONString(subjectInfoDTO));
            }
            Preconditions.checkArgument(!StringUtils.isBlank(subjectInfoDTO.getSubjectName()),"题目名不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectDifficult(),"题目难度不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectType(),"题目类型不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectScore(),"题目分数不能为空");
            Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectInfoDTO.getCategoryIds()),"分类id不能为空");
            Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectInfoDTO.getLabelIds()),"标签id不能为空");
            //DTO转换为BO
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter
                    .INSTANCE.convertInfoDTOToBO(subjectInfoDTO);
            List<SubjectAnswerBO> subjectAnswerBOList = SubjectAnswerDTOConverter
                    .INSTANCE.subjectAnswerDTOToBOList(subjectInfoDTO.getOptionList());
            subjectInfoBO.setOptionList(subjectAnswerBOList);
         subjectInfoDomainService.add(subjectInfoBO);
             return Result.success(true);
        } catch (Exception e) {
            log.error("SubjectController.add.error:{}",e.getMessage(),e);
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 查询题目列表
     * @param subjectInfoDTO
     * @return
     */
    @PostMapping("/getSubjectPage")
    public Result<PageResult<SubjectInfoDTO>> getSubjectPage(@RequestBody SubjectInfoDTO subjectInfoDTO){
        try {
            if(log.isInfoEnabled()){
                log.info("SubjectController.getSubjectPage.dto:{}", JSON.toJSONString(subjectInfoDTO));
            }
            Preconditions.checkNotNull(subjectInfoDTO.getCategoryId(),"分类id不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getLabelId(),"标签id不能为空");
            //DTO转换为BO
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter
                    .INSTANCE.convertInfoDTOToBO(subjectInfoDTO);
            PageResult<SubjectInfoBO> boPageResult = subjectInfoDomainService.getSubjectPage(subjectInfoBO);
            return Result.success(boPageResult);
        } catch (Exception e) {
            log.error("SubjectController.getSubjectPage.error:{}",e.getMessage(),e);
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 查询题目信息
     * @param subjectInfoDTO
     * @return
     */
    @PostMapping("/querySubjectInfo")
    public Result<SubjectInfoDTO> querySubjectInfo(@RequestBody SubjectInfoDTO subjectInfoDTO){
        try {
            if(log.isInfoEnabled()){
                log.info("SubjectController.querySubjectInfo.dto:{}", JSON.toJSONString(subjectInfoDTO));
            }
            Preconditions.checkNotNull(subjectInfoDTO.getId(),"题目id不能为空");

            //DTO转换为BO
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter
                    .INSTANCE.convertInfoDTOToBO(subjectInfoDTO);
            SubjectInfoBO boResult = subjectInfoDomainService.querySubjectInfo(subjectInfoBO);
            SubjectInfoDTO dtoResult = SubjectInfoDTOConverter
                    .INSTANCE.convertInfoBOToDTO(boResult);
            return Result.success(dtoResult);
        } catch (Exception e) {
            log.error("SubjectController.querySubjectInfo.error:{}",e.getMessage(),e);
            return Result.fail(e.getMessage());
        }
    }



}
